home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / mpeg_stat-2.2 / util.h < prev    next >
C/C++ Source or Header  |  1995-05-10  |  24KB  |  400 lines

  1. /* MPEGSTAT - analyzing tool for MPEG-I video streams
  2.  * 
  3.  *  Copyright (c) 1995 The Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Technical University of Berlin, Germany, Dept. of Computer Science
  7.  * Tom Pfeifer - Multimedia systems project - pfeifer@fokus.gmd.de
  8.  *
  9.  * Jens Brettin, Harald Masche, Alexander Schulze, Dirk Schubert
  10.  *
  11.  * This program uses parts of the source code of the Berkeley MPEG player
  12.  *
  13.  * ---------------------------
  14.  *
  15.  * Copyright (c) 1993 Technical University of Berlin, Germany
  16.  *
  17.  * for the parts of the Berkeley player used:
  18.  *
  19.  * Copyright (c) 1992 The Regents of the University of California.
  20.  * All rights reserved.
  21.  *
  22.  * ---------------------------
  23.  *
  24.  * Permission to use, copy, modify, and distribute this software and its
  25.  * documentation for any purpose, without fee, and without written agreement is
  26.  * hereby granted, provided that the above copyright notices and the following
  27.  * two paragraphs appear in all copies of this software.
  28.  * 
  29.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA 
  30.  * or the Technical University of Berlin BE LIABLE TO ANY PARTY FOR
  31.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  32.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  33.  * CALIFORNIA or the Technical University of Berlin HAS BEEN ADVISED OF THE 
  34.  * POSSIBILITY OF SUCH DAMAGE.
  35.  * 
  36.  * THE UNIVERSITY OF CALIFORNIA and the Technical University of Berlin 
  37.  * SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  38.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39.  * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE 
  40.  * UNIVERSITY OF CALIFORNIA and the Technical University of Berlin HAVE NO 
  41.  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
  42.  * OR MODIFICATIONS.
  43.  */
  44. /* Status codes for bit stream i/o operations. */
  45.  
  46. #define NO_VID_STREAM    (-1)
  47. #define STREAM_UNDERFLOW (-2)
  48. #define OK 1
  49.  
  50. /* Size increment of extension data buffers. */
  51.  
  52. #define EXT_BUF_SIZE 1024
  53.  
  54. /* External declarations for bitstream i/o operations. */
  55. extern unsigned int bitMask[];
  56. extern unsigned int nBitMask[];
  57. extern unsigned int rBitMask[];
  58. extern unsigned int bitTest[];
  59.  
  60. /* External declarations of bitstream global variables. */
  61. extern unsigned int curBits;
  62. extern int bitOffset;
  63. extern int bufLength;
  64. extern unsigned int *bitBuffer;
  65.  
  66. /* Macro for updating bit counter */
  67. #define UPDATE_COUNT(numbits) bitCount += numbits
  68.  
  69. #ifdef NO_SANITY_CHECKS
  70. #define get_bits1(result)                                                 \
  71. {                                                                         \
  72.   UPDATE_COUNT(1);                                                        \
  73.   result = ((curBits & 0x80000000) != 0);                              \
  74.   curBits <<= 1;                                                          \
  75.   bitOffset++;                                                            \
  76.                                                                           \
  77.   if (bitOffset & 0x20) {                                                 \
  78.     bitOffset = 0;                                                        \
  79.     bitBuffer++;                                                          \
  80.     curBits = *bitBuffer;                                                 \
  81.     bufLength--;                                                          \
  82.   }                                                                       \
  83. }
  84.  
  85. #define get_bits2(result)                                                 \
  86. {                                                                         \
  87.   UPDATE_COUNT(2);                                                        \
  88.   bitOffset += 2;                                                         \
  89.                                                                           \
  90.   if (bitOffset & 0x20) {                                                 \
  91.     bitOffset -= 32;                                                      \
  92.     bitBuffer++;                                                          \
  93.     bufLength--;                                                          \
  94.     if (bitOffset) {                                                      \
  95.       curBits |= (*bitBuffer >> (2 - bitOffset));                         \
  96.     }                                                                     \
  97.     result = ((curBits & 0xc0000000) >> 30);                           \
  98.     curBits = *bitBuffer << bitOffset;                                    \
  99.   }                                                                       \
  100.                                                                           \
  101.   result = ((curBits & 0xc0000000) >> 30);                             \
  102.   curBits <<= 2;                                                          \
  103. }
  104.  
  105. #define get_bitsX(num, mask, shift,  result)                              \
  106. {                                                                         \
  107.   UPDATE_COUNT(num);                                                      \
  108.   bitOffset += num;                                                       \
  109.                                                                           \
  110.   if (bitOffset & 0x20) {                                                 \
  111.     bitOffset -= 32;                                                      \
  112.     bitBuffer++;                                                          \
  113.     bufLength--;                                                          \
  114.     if (bitOffset) {                                                      \
  115.       curBits |= (*bitBuffer >> (num - bitOffset));                       \
  116.     }                                                                     \
  117.     result = ((curBits & mask) >> shift);                              \
  118.     curBits = *bitBuffer << bitOffset;                                    \
  119.   }                                                                       \
  120.   else {                                                                  \
  121.     result = ((curBits & mask) >> shift);                              \
  122.     curBits <<= num;                                                      \
  123.   }                                                                       \
  124. }
  125. #define srsget_bitsX(num, mask, shift,  result)                              \
  126. {                                                                         \
  127.   UPDATE_COUNT(num);                                                      \
  128.   bitOffset += num;                                                       \
  129.                                                                           \
  130.   if (bitOffset & 0x20) {                                                 \
  131.     bitOffset -= 32;                                                      \
  132.     bitBuffer++;                                                          \
  133.     bufLength--;                                                          \
  134.       curBits |= (*bitBuffer >> (num - bitOffset));                       \
  135.     result = ((curBits & mask) >> shift);                              \
  136.     curBits = *bitBuffer << bitOffset;                                    \
  137.   }                                                                       \
  138.   else {                                                                  \
  139.     result = ((curBits & mask) >> shift);                              \
  140.     curBits <<= num;                                                      \
  141.   }                                                                       \
  142. }
  143. #else
  144.  
  145. #define get_bits1(result)                                                 \
  146. {                                                                         \
  147.   /* Check for underflow. */                                              \
  148.                                                                           \
  149.   if (bufLength < 2) {                                                    \
  150.     correct_underflow();                                                  \
  151.   }                                                                       \
  152.   UPDATE_COUNT(1);                                                        \
  153.   result = ((curBits & 0x80000000) != 0);                              \
  154.   curBits <<= 1;                                                          \
  155.   bitOffset++;                                                            \
  156.                                                                           \
  157.   if (bitOffset & 0x20) {                                                 \
  158.     bitOffset = 0;                                                        \
  159.     bitBuffer++;                                                          \
  160.     curBits = *bitBuffer;                                                 \
  161.     bufLength--;                                                          \
  162.   }                                                                       \
  163. }
  164.  
  165. #define get_bits2(result)                                                 \
  166. {                                                                         \
  167.   /* Check for underflow. */                                              \
  168.                                                                           \
  169.   if (bufLength < 2) {                                                    \
  170.     correct_underflow();                                                  \
  171.   }                                                                       \
  172.   UPDATE_COUNT(2);                                                        \
  173.   bitOffset += 2;                                                         \
  174.                                                                           \
  175.   if (bitOffset & 0x20) {                                                 \
  176.     bitOffset -= 32;                                                      \
  177.     bitBuffer++;                                                          \
  178.     bufLength--;                                                          \
  179.     if (bitOffset) {                                                      \
  180.       curBits |= (*bitBuffer >> (2 - bitOffset));                         \
  181.     }                                                                     \
  182.     result = ((curBits & 0xc0000000) >> 30);                           \
  183.     curBits = *bitBuffer << bitOffset;                                    \
  184.   }                                                                       \
  185.                                                                           \
  186.   result = ((curBits & 0xc0000000) >> 30);                             \
  187.   curBits <<= 2;                                                          \
  188. }
  189.  
  190. #define get_bitsX(num, mask, shift,  result)                              \
  191. {                                                                         \
  192.   /* Check for underflow. */                                              \
  193.                                                                           \
  194.   if (bufLength < 2) {                                                    \
  195.     correct_underflow();                                                  \
  196.   }                                                                       \
  197.   UPDATE_COUNT(num);                                                      \
  198.   bitOffset += num;                                                       \
  199.                                                                           \
  200.   if (bitOffset & 0x20) {                                                 \
  201.     bitOffset -= 32;                                                      \
  202.     bitBuffer++;                                                          \
  203.     bufLength--;                                                          \
  204.     if (bitOffset) {                                                      \
  205.       curBits |= (*bitBuffer >> (num - bitOffset));                       \
  206.     }                                                                     \
  207.     result = ((curBits & mask) >> shift);                              \
  208.     curBits = *bitBuffer << bitOffset;                                    \
  209.   }                                                                       \
  210.   else {                                                                  \
  211.    result = ((curBits & mask) >> shift);                               \
  212.    curBits <<= num;                                                       \
  213.   }                                                                       \
  214. }
  215. #endif
  216.  
  217. #define get_bits3(result) get_bitsX(3,   0xe0000000, 29, result)
  218. #define get_bits4(result) get_bitsX(4,   0xf0000000, 28, result)
  219. #define get_bits5(result) get_bitsX(5,   0xf8000000, 27, result)
  220. #define get_bits6(result) get_bitsX(6,   0xfc000000, 26, result)
  221. #define get_bits7(result) get_bitsX(7,   0xfe000000, 25, result)
  222. #define get_bits8(result) get_bitsX(8,   0xff000000, 24, result)
  223. #define get_bits9(result) get_bitsX(9,   0xff800000, 23, result)
  224. #define get_bits10(result) get_bitsX(10, 0xffc00000, 22, result)
  225. #define get_bits11(result) get_bitsX(11, 0xffe00000, 21, result)
  226. #define get_bits12(result) get_bitsX(12, 0xfff00000, 20, result)
  227. #define get_bits14(result) get_bitsX(14, 0xfffc0000, 18, result)
  228. #define get_bits16(result) get_bitsX(16, 0xffff0000, 16, result)
  229. #define get_bits18(result) get_bitsX(18, 0xffffc000, 14, result)
  230. #define get_bits32(result) get_bitsX(32, 0xffffffff,  0, result)
  231.  
  232. #define get_bitsn(num, result) get_bitsX((num), nBitMask[num], (32-(num)), result)
  233.  
  234. #ifdef NO_SANITY_CHECKS
  235. #define show_bits32(result)                                      \
  236. {                                                                       \
  237.   if (bitOffset) {                            \
  238.     result = curBits | (*(bitBuffer+1) >> (32 - bitOffset));        \
  239.   }                                                                     \
  240.   else {                                                                \
  241.     result = curBits;                            \
  242.   }                                                                     \
  243. }
  244.  
  245. #define show_bitsX(num, mask, shift,  result)                           \
  246. {                                                                       \
  247.   int bO;                                                               \
  248.   bO = bitOffset + num;                                                 \
  249.   if (bO > 32) {                                                        \
  250.     bO -= 32;                                                           \
  251.     result = ((curBits & mask) >> shift) |                              \
  252.                 (*(bitBuffer+1) >> (shift + (num - bO)));               \
  253.   }                                                                     \
  254.   else {                                                                \
  255.     result = ((curBits & mask) >> shift);                               \
  256.   }                                                                     \
  257. }
  258.  
  259. #else
  260. #define show_bits32(result)                                       \
  261. {                                                                       \
  262.   /* Check for underflow. */                                            \
  263.   if (bufLength < 2) {                                                  \
  264.     correct_underflow();                                                \
  265.   }                                                                     \
  266.   if (bitOffset) {                            \
  267.     result = curBits | (*(bitBuffer+1) >> (32 - bitOffset));        \
  268.   }                                                                     \
  269.   else {                                                                \
  270.     result = curBits;                            \
  271.   }                                                                     \
  272. }
  273.  
  274. #define show_bitsX(num, mask, shift, result)                            \
  275. {                                                                       \
  276.   int bO;                                                               \
  277.                                                                         \
  278.   /* Check for underflow. */                                            \
  279.   if (bufLength < 2) {                                                  \
  280.     correct_underflow();                                                \
  281.   }                                                                     \
  282.   bO = bitOffset + num;                                                 \
  283.   if (bO > 32) {                                                        \
  284.     bO -= 32;                                                           \
  285.     result = ((curBits & mask) >> shift) |                              \
  286.                 (*(bitBuffer+1) >> (shift + (num - bO)));               \
  287.   }                                                                     \
  288.   else {                                                                \
  289.     result = ((curBits & mask) >> shift);                               \
  290.   }                                                                     \
  291. }
  292. #endif
  293.  
  294. #define show_bits1(result)  show_bitsX(1,  0x80000000, 31, result)
  295. #define show_bits2(result)  show_bitsX(2,  0xc0000000, 30, result)
  296. #define show_bits3(result)  show_bitsX(3,  0xe0000000, 29, result)
  297. #define show_bits4(result)  show_bitsX(4,  0xf0000000, 28, result)
  298. #define show_bits5(result)  show_bitsX(5,  0xf8000000, 27, result)
  299. #define show_bits6(result)  show_bitsX(6,  0xfc000000, 26, result)
  300. #define show_bits7(result)  show_bitsX(7,  0xfe000000, 25, result)
  301. #define show_bits8(result)  show_bitsX(8,  0xff000000, 24, result)
  302. #define show_bits9(result)  show_bitsX(9,  0xff800000, 23, result)
  303. #define show_bits10(result) show_bitsX(10, 0xffc00000, 22, result)
  304. #define show_bits11(result) show_bitsX(11, 0xffe00000, 21, result)
  305. #define show_bits12(result) show_bitsX(12, 0xfff00000, 20, result)
  306. #define show_bits13(result) show_bitsX(13, 0xfff80000, 19, result)
  307. #define show_bits14(result) show_bitsX(14, 0xfffc0000, 18, result)
  308. #define show_bits15(result) show_bitsX(15, 0xfffe0000, 17, result)
  309. #define show_bits16(result) show_bitsX(16, 0xffff0000, 16, result)
  310. #define show_bits17(result) show_bitsX(17, 0xffff8000, 15, result)
  311. #define show_bits18(result) show_bitsX(18, 0xffffc000, 14, result)
  312. #define show_bits19(result) show_bitsX(19, 0xffffe000, 13, result)
  313. #define show_bits20(result) show_bitsX(20, 0xfffff000, 12, result)
  314. #define show_bits21(result) show_bitsX(21, 0xfffff800, 11, result)
  315. #define show_bits22(result) show_bitsX(22, 0xfffffc00, 10, result)
  316. #define show_bits23(result) show_bitsX(23, 0xfffffe00,  9, result)
  317. #define show_bits24(result) show_bitsX(24, 0xffffff00,  8, result)
  318. #define show_bits25(result) show_bitsX(25, 0xffffff80,  7, result)
  319. #define show_bits26(result) show_bitsX(26, 0xffffffc0,  6, result)
  320. #define show_bits27(result) show_bitsX(27, 0xffffffe0,  5, result)
  321. #define show_bits28(result) show_bitsX(28, 0xfffffff0,  4, result)
  322. #define show_bits29(result) show_bitsX(29, 0xfffffff8,  3, result)
  323. #define show_bits30(result) show_bitsX(30, 0xfffffffc,  2, result)
  324. #define show_bits31(result) show_bitsX(31, 0xfffffffe,  1, result)
  325.  
  326. #define show_bitsn(num,result) show_bitsX((num), (0xffffffff << (32-(num))), (32-(num)), result)
  327.  
  328. #ifdef NO_SANITY_CHECKS
  329. #define flush_bits32                                                  \
  330. {                                                                     \
  331.   UPDATE_COUNT(32);                                                   \
  332.                                                                       \
  333.   bitBuffer++;                                                        \
  334.   bufLength--;                                                        \
  335.   curBits = *bitBuffer  << bitOffset;                                 \
  336. }
  337.  
  338. #define flush_bits(num)                                               \
  339. {                                                                     \
  340.   bitOffset += num;                                                   \
  341.                                                                       \
  342.   UPDATE_COUNT(num);                                                  \
  343.                                                                       \
  344.   if (bitOffset & 0x20) {                                             \
  345.     bitOffset -= 32;                                                  \
  346.     bitBuffer++;                                                      \
  347.     bufLength--;                                                      \
  348.     curBits = *bitBuffer  << bitOffset;                               \
  349.   }                                                                   \
  350.   else {                                                              \
  351.     curBits <<= num;                                                  \
  352.   }                                                                   \
  353. }
  354. #else
  355. #define flush_bits32                                                  \
  356. {                                                                     \
  357.   if (curVidStream == NULL) {                                         \
  358.     /* Deal with no vid stream here. */                               \
  359.   }                                                                   \
  360.                                                                       \
  361.   if (bufLength < 2) {                                                \
  362.     correct_underflow();                                              \
  363.   }                                                                   \
  364.                                                                       \
  365.   UPDATE_COUNT(32);                                                   \
  366.                                                                       \
  367.   bitBuffer++;                                                        \
  368.   bufLength--;                                                        \
  369.   curBits = *bitBuffer  << bitOffset;                                 \
  370. }
  371.  
  372. #define flush_bits(num)                                               \
  373. {                                                                     \
  374.   if (curVidStream == NULL) {                                         \
  375.     /* Deal with no vid stream here. */                               \
  376.   }                                                                   \
  377.                                                                       \
  378.   if (bufLength < 2) {                                                \
  379.     correct_underflow();                                              \
  380.   }                                                                   \
  381.                                                                       \
  382.   UPDATE_COUNT(num);                                                  \
  383.                                                                       \
  384.   bitOffset += num;                                                   \
  385.                                                                       \
  386.   if (bitOffset & 0x20) {                                             \
  387.     bufLength--;                                                      \
  388.     bitOffset -= 32;                                                  \
  389.     bitBuffer++;                                                      \
  390.     curBits = *bitBuffer << bitOffset;                                \
  391.   }                                                                   \
  392.   else {                                                              \
  393.     curBits <<= num;                                                  \
  394.   }                                                                   \
  395. }
  396. #endif
  397.  
  398. #define UTIL2
  399.  
  400.